package cn.seaboot.admin.image;

import cn.seaboot.commons.file.FilenameUtils;
import cn.seaboot.commons.file.WriterUtils;
import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.CannotWriteException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.Tag;
import org.jaudiotagger.tag.TagException;
import org.jaudiotagger.tag.datatype.Artwork;
import org.jaudiotagger.tag.id3.ID3v24Tag;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.Arrays;

/**
 * @author Mr.css
 * @version 2025-03-04 11:36
 */
public class TestMp3 {

    public static void main(String[] args) {
        // 建瓯G205线
        // 建瓯G237线K1531+930-K1555+570
        // 建瓯G237线K1489+410-K1516+132段灾害防治工程
        // 建瓯G237线K1516+300-K1530+042段灾害防治工程
        // 建瓯G505线

        File root = new File("D:\\maker\\gl-nms\\images\\建瓯G205线");
        File[] files = root.listFiles();

        for (File file : files) {
            try {
                if(file.isDirectory()) {
                    System.out.println(file.getName());
                    list(file);
                }
            } catch (Exception e) {
                continue;
            }
        }
    }

    private static void list(File root) {
        File[] files = root.listFiles();
        for (File file : files) {
            if(file.isFile()) {
                System.out.println(file.getName());
            }
        }
    }

    public static void main4(String[] args) {
        File root = new File("D:\\vedio\\【AI阿梓 粤梓】");
        String[] names = root.list();

        for (String name : names) {
            if (!name.endsWith(".aac")) {
                continue;
            }
            System.out.println(name);

            int i1 = name.indexOf("【");
            int i2 = name.indexOf("】 - ", i1 + 1);
            int i3 = name.indexOf(" - ", i2 + 3);
            int i4 = name.indexOf(".", i3 + 1);


            String f1 = name.substring(0, i1);
            String f2 = name.substring(i1 + 1, i2);
            String f3 = name.substring(i2 + 4, i3);
            String f4 = name.substring(i3 + 3, i4);

//            String ret = f1 + f2 + f3 + f4 + ".aac";
            String ret = "AI阿梓 - " + f3 + ".aac";
//            String ret = "AI阿梓 - " + f2 + "《" + f3 + "》" + ".aac";
            System.out.println(ret);


            new File(root, name).renameTo(new File(root, ret));
        }
    }


    public static void main3(String[] args) {
        File root = new File("D:\\mp3\\阿梓【儿歌合集】");
        String[] names = root.list();

        for (String name : names) {
            System.out.println(name);
            String ret = name.replace("儿歌", "童声奶梓歌合集");
            new File(root, name).renameTo(new File(root, ret));
        }
    }


    public static void main2(String[] args) throws CannotReadException, TagException, InvalidAudioFrameException, ReadOnlyFileException, IOException, CannotWriteException {
        File root = new File("D:\\mp3\\【AI阿梓 流行歌】");
        String[] names = root.list();

        for (String name : names) {
            if (!name.endsWith(".mp3")) {
                continue;
            }
            System.out.println(name);

            String fn = FilenameUtils.getName(name);
            String ex = FilenameUtils.getExtension(name);

            String[] f = fn.substring(0, fn.lastIndexOf('.')).split(" - ");
//            String[] f = fn.split("[-|『|』]");
            System.out.println(Arrays.toString(f));

            File file = new File(root, name);
            AudioFile audioFile = AudioFileIO.read(file);
            Tag tag = audioFile.getTag();
            if (tag == null) {
                tag = new ID3v24Tag();
                audioFile.setTag(tag);
            }

            System.out.println(tag.getClass());

            // 设置作者（艺术家）信息
            tag.setField(FieldKey.ARTIST, "AI阿梓");
            // 设置专辑信息
            tag.setField(FieldKey.ALBUM, "AI阿梓 流行歌");
            // 设置标题信息
//            tag.setField(FieldKey.TITLE, f2 + "《" + f3 + "》");
            tag.setField(FieldKey.TITLE, f[1]);

            tag.setField(FieldKey.YEAR, "2023");

            Artwork artwork = Artwork.createArtworkFromFile(new File(root, "b.jpg"));
            tag.setField(artwork);

            audioFile.commit();

            // 保存修改后的文件
            AudioFileIO.write(audioFile);
        }
    }


    public static void main12(String[] args) throws IOException {
        File root = new File("D:\\vedio");
        String[] names = root.list();

        for (String name : names) {
            File f = new File(root, name);
            if (f.isDirectory()) {
                genBatFile(f);
            }
        }
    }

    public static void genBatFile(File root) throws IOException {
        String[] names = root.list();

        File file = new File(root, "a.bat");
        if (!file.exists()) {
            file.createNewFile();
        }
        StringBuilder sb = new StringBuilder();
        sb.append("@chcp 65001 > nul\r\n");

        assert names != null;
        for (String name : names) {
            if (!name.endsWith(".aac")) {
                continue;
            }
            sb.append("ffmpeg -i \"").append(name).append("\" -c:a libmp3lame \"").append(name.replace(".aac", ".mp3")).append("\"\r\n");
        }

        try (OutputStream os = Files.newOutputStream(file.toPath())) {
            WriterUtils.write(os, sb.toString(), "UTF-8");
        }
    }

}
